#include "myprogressbarmodel.h"
#include <QVariant>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>

MyProgressBarModel::MyProgressBarModel(QObject *parent)
    : QAbstractListModel(parent)
{
    connect(this, SIGNAL(myDataChanged(const QString&)), this, SLOT(updateMydata(const QString&)));
}

MyProgressBarModel::~MyProgressBarModel()
{

}

void MyProgressBarModel::insert(int index, const MyData &value)
{
    if(index < 0 || index > m_listData.count()) {
        return;
    }

    QString temp(value.m_strUuid);
    temp = temp.trimmed();
    if (temp.isEmpty() || temp.isNull())
        return;

    emit beginInsertRows(QModelIndex(), index, index);
    m_listData.insert(index, value);
    emit endInsertRows();
    emit countChanged(m_listData.count());
    emit dataChanged(QModelIndex(), QModelIndex());
}

void MyProgressBarModel::modified(int index, const MyData &value)
{
    if(index < 0 || index > m_listData.count()) {
        return;
    }

    QString temp(value.m_strUuid);
    temp = temp.trimmed();
    if (temp.isEmpty() || temp.isNull())
        return;

    remove(index);
    insert(index, value);
}

void MyProgressBarModel::append(const MyData &value)
{
    insert(0, value);
}

void MyProgressBarModel::remove(int index)
{
    if(index < 0 || index >= m_listData.count()) {
        return;
    }

    emit beginRemoveRows(QModelIndex(), index, index);
    m_listData.removeAt(index);
    emit endRemoveRows();
    emit countChanged(m_listData.count());
}

void MyProgressBarModel::clear()
{
    emit beginResetModel();
    m_listData.clear();
    emit endResetModel();
}

QString MyProgressBarModel::getText(int index)
{
    if(index < 0 || index >= m_listData.count()) {
        return "";
    }
    return m_listData.at(index).text();
}

float MyProgressBarModel::getValue(int index)
{
    if(index < 0 || index >= m_listData.count()) {
        return 0;
    }

    auto& value = m_listData.at(index);
    return (float)value.step() / (float)value.total();
}

int MyProgressBarModel::getIndex(const QString &uuid)
{
    int index = 0;
    for ( auto& i : m_listData)
    {
        if (i.uuid() == uuid)
        {
            return index;
        }
        ++index;
    }

    return -1;
}

int MyProgressBarModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_listData.count();
}

QVariant MyProgressBarModel::data(const QModelIndex &index, int role) const
{
    // the index returns the requested row and column information.
    // we ignore the column and only use the row information
    int row = index.row();

    // boundary check for the row
    if(row < 0 || row >= m_listData.count()) {
        return QVariant();
    }

    // A model can return data for different roles.
    // The default role is the display role.
    // it can be accesses in QML with "model.display"
    switch(role) {
        case Qt::DisplayRole:
            // Return the color name for the particular row
            // Qt automatically converts it to the QVariant type
            return m_listData.value(row).text();
    }

    // The view asked for other data, just return an empty QVariant
    return QVariant();
}

void MyProgressBarModel::updateMydata(const QString &json)
{
    QString value(json);
    QJsonParseError parseJsonErr;
    QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(),&parseJsonErr);
    if(!(parseJsonErr.error == QJsonParseError::NoError))
    {
        return;
    }

    QJsonObject jsonObject = document.object();
    MyData data;

    data.m_nStep = jsonObject["step"].toInt();
    data.m_nTotal = jsonObject["total"].toInt();
    data.m_strUuid = jsonObject["uuid"].toString();
    data.m_strText = jsonObject["text"].toString();

//    qDebug() << "text:" << jsonObject["text"].toString() << endl;
//    qDebug() << "uuid:" << jsonObject["uuid"].toString() << endl;
//    qDebug() << "step:" << jsonObject["step"].toInt() << endl;
//    qDebug() << "total:" << jsonObject["total"].toInt() << endl;

    int index = getIndex(data.m_strUuid);
    if (index != -1)
    {
        modified(index, data);
    }
    else
    {
        append(data);
    }
}

int MyProgressBarModel::count() const
{
    return rowCount(QModelIndex());
}
